package com.example.ql.express.model.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.example.ql.express.config.handler.JsonStringArrayTypeHandler;
import com.example.ql.express.model.enums.AppendRuleEnum;
import com.example.ql.express.model.enums.ClassEnum;
import com.example.ql.express.model.enums.OperateEnum;
import com.example.ql.express.model.vo.ScriptConfigVO;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.List;

/**
 * @author xuweizhi
 * @since 2021/10/29 00:50
 */
@Data
@TableName(value = "script_config_relationship",autoResultMap = true)
public class ScriptConfigDO implements Serializable {

    /**
     * 主键
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 字段名
     */
    @TableField(typeHandler = JsonStringArrayTypeHandler.class)
    private List<String> filedName;

    /**
     * 字段类型
     */
    private ClassEnum filedType;

    /**
     * 比较规则
     */
    private String compareRule;

    /**
     * 校验规则
     */
    @TableField(typeHandler = JsonStringArrayTypeHandler.class)
    private List<String> validateRule;
    /**
     * 分诊等级
     */
    @TableField(typeHandler = JsonStringArrayTypeHandler.class)
    private List<String> levels;
    /**
     * 父级引用主键: 以此分组
     */
    private String parentPath;

    /**
     * 操作类型
     */
    private OperateEnum operate;

    /**
     * 拼接规则
     */
    private AppendRuleEnum appendRule;

    /**
     * tab 缩进间隔
     */
    @TableField(exist = false)
    private String reservedTable;

    public ScriptConfigVO of(){
        ScriptConfigVO scriptConfigVO = new ScriptConfigVO();
        BeanUtils.copyProperties(this, scriptConfigVO);
        return scriptConfigVO;
    }


    // public static class ValidateRuleTypeHandler implements TypeHandler<List<String>> {
    //
    //     @Override
    //     public void setParameter(PreparedStatement preparedStatement, int i, List<String> list, JdbcType jdbcType) throws SQLException {
    //         preparedStatement.setString(i, JSON.toJSONString(list));
    //     }
    //
    //     @Override
    //     public List<String> getResult(ResultSet resultSet, String s) throws SQLException {
    //         String result = resultSet.getString(s);
    //         return JSON.parseArray(result, String.class);
    //     }
    //
    //     @Override
    //     public List<String> getResult(ResultSet resultSet, int i) throws SQLException {
    //         String result = resultSet.getString(i);
    //         return JSON.parseArray(result, String.class);
    //     }
    //
    //     @Override
    //     public List<String> getResult(CallableStatement callableStatement, int i) throws SQLException {
    //         String result = callableStatement.getString(i);
    //         return JSON.parseArray(result, String.class);
    //     }
    // }

}
